//index.js
//获取应用实例
let pNo = 1;
const getToken= require('../../utils/get_token.js');
const getMemId = require('../../utils/memberId.js');
let openId = '', memberId = '', ctId='';
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getClass(that);
  if (openId == '') {
    getToken.getOpenId(function (res) {
      wx.setStorageSync('openId', res.data.openId);
      if (memberId == '') {
        getMemId.getMemId(res.data.openId, function (res) {
          if (res.data.flag == 'true') {
            wx.setStorageSync('memberId', res.data.memberId);
          }
        })
      }
    });
  }
}
const getClass = function (that) {//分类
  let params = [ 'shop/pptIco/getDictionaryValueList', {"dicType":"SLP_CATEGRAY_LIST"}]
  getToken.getData(function (res) {
    let data = res.data;
    console.log(data);
    data.rows[0].show = true;
    that.data.fList = data.rows;
    ctId = that.data.fList[0].showName;
    indexList(that, pNo)//初始加载
    that.setData({
      fList: that.data.fList
    })
  }, ...params)

}
const indexList = (that, pNo) => {//商品列表
  if (that.data.boolT) {
    that.setData({
      hidden: false
    })
    that.data.boolT = false;
    let params = [ 'shop/pingTuan/getPinTuanIngByCategoryId', {"pageNo":pNo,"categoryId": ctId }]
    getToken.getData(function (res) {
      let data = res.data;
      if (data.rows.length > 0) {
        for (let i = 0; i < data.rows.length; i++) {
         // data.rows[i].imageStore = data.rows[i].imageStore.substring(1, data.rows[i].imageStore.length - 1);
          data.rows[i].imageStore = JSON.parse(data.rows[i].imageStore);
          data.rows[i].img = 'https://www.globalhexi.com/' + data.rows[i].imageStore[0].path + '/small/' + data.rows[i].imageStore[0].id + '.jpg';
          data.rows[i].intPinPrice = (data.rows[i].intPinPrice / 100).toFixed(2);
          that.data.list.push(data.rows[i]);
        }
      } else {
        if (pNo == 1) {
          that.data.hasShow = false;
        }
      }
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          list: that.data.list,
          hasShow: that.data.hasShow,
          hidden: true
        })
      }, 500)
    }, ...params)
   }
};

Page({
  data: {
    fList: [],
    hidden: false,
    boolT: true,
    hasShow:true,
    wHeight:0,
    list: []

  },
  onLoad: function () {
    let that = this;
    pNo= 1;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    wx.getSystemInfo({
      success(res) {
        that.data.wHeight = res.windowHeight;
        that.setData({
          wHeight: that.data.wHeight
        })
      }
    })
    ckToken(that);
  },
  go_sd: function (e) {//分类切换
    let that = this;
    let showName = e.currentTarget.dataset.id;
    for (let i = 0; i < that.data.fList.length; i++) {
      if (that.data.fList[i].showName == showName) {
        that.data.fList[i].show = true;
      } else {
        that.data.fList[i].show = false;
      }
    }
    that.setData({
      fList: that.data.fList
    })
    ctId = showName;
    pNo = 1;
    that.data.hasShow=true;
    that.data.list = [];
    indexList(that, pNo);
  },
  go_detail: function (e) {//直接去详情
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  onReachBottom: function () {//上啦加载更多
    let that = this;
    pNo++;
    indexList(that, pNo);
  },
  onPullDownRefresh: function (e) {//下拉刷新
    let that = this;
    wx.showNavigationBarLoading()//加载动画
    pNo = 1;
    that.data.list = [];
    indexList(that, pNo);
    setTimeout(function () {
      wx.hideNavigationBarLoading()//隐藏动画
      wx.stopPullDownRefresh()//停止加载
    }, 500)

  }
})
